package com.xiaorun.portal.controller.admin;

import com.github.pagehelper.PageInfo;
import com.xiaorun.portal.constant.WebConst;
import com.xiaorun.portal.controller.BaseController;
import com.xiaorun.portal.dto.Types;
import com.xiaorun.portal.exception.TipException;
import com.xiaorun.portal.modal.Bo.RestResponseBo;
import com.xiaorun.portal.modal.Vo.AttachVo;
import com.xiaorun.portal.service.IAttachService;
import com.xiaorun.portal.service.ILogService;
import com.xiaorun.portal.utils.Commons;
import com.xiaorun.portal.utils.QiNiuImgUpload;
import com.xiaorun.portal.utils.TaleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 附件管理
 *
 * Created by 13 on 2018/2/21.
 */
@Controller
@RequestMapping("admin/qiniu")
@Slf4j
public class QiNiuAttachController extends BaseController {

    public static final String CLASSPATH = TaleUtils.getUplodFilePath();

    @Resource
    private IAttachService attachService;

    @Resource
    private ILogService logService;

    @Autowired
    private QiNiuImgUpload qiNiuImgUpload;

    /**
     * 附件页面
     *
     * @param request
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "")
    public String index(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "12") int limit) {
        PageInfo<AttachVo> attachPaginator = attachService.getAttachs(page, limit);
        request.setAttribute("attachs", attachPaginator);
        request.setAttribute(Types.ATTACH_URL.getType(), Commons.site_option(Types.ATTACH_URL.getType(), Commons.site_url()));
        request.setAttribute("max_file_size", WebConst.MAX_FILE_SIZE / 1024);
        return "admin/attach";
    }

    /**
     *  CKEditor 5 上传图片接口
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/uploadImg")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponseBo uploadImg(HttpServletRequest request, @RequestParam("file") MultipartFile[] multipartFiles) throws IOException {
        Map oMap = new HashMap();
        try {
            for (MultipartFile multipartFile : multipartFiles) {
                if (multipartFile.getSize() <= WebConst.MAX_FILE_SIZE) {
                    String url = qiNiuImgUpload.uploadImg(multipartFile);
                    oMap.put("url",  url);
                }else {
                    oMap.put("msg",  "图片大小不能超过1M");
                }
            }
        } catch (Exception e) {
            return RestResponseBo.fail();
        }
        return RestResponseBo.ok(oMap);
    }


}
